<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="接口名称" prop="drname">
        <el-input
          v-model="queryParams.drname"
          placeholder="请输入接口设备名称"
          clearable
          @keyup.enter.native="handleQuery"
          id="drname"
        />
      </el-form-item>
      <el-form-item label="接口驱动" prop="drllid">
        <el-select v-model="queryParams.drllid" placeholder="请选择接口驱动" id="drllid" @change="handleQuery" clearable>
          <el-option
            v-for="apptype in drllOptions"
            :key="apptype.drllid"
            :label="apptype.drllname"
            :value="apptype.drllid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="驱动类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="驱动类型" id="type" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_dll_type"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="ip和端口" prop="ipport">
        <el-input
          v-model="queryParams.ipport"
          placeholder="格式192.168.2.200:30"
          clearable
          @keyup.enter.native="handleQuery"
          id="ipport"
        />
      </el-form-item>
      <el-form-item label="设备串口" prop="com">
        <el-select
          v-model="queryParams.com"
          placeholder="设备串口"
          @change="handleQuery" clearable id="com"
        >
          <el-option
            v-for="dict in dict.type.sys_com"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="波特率" prop="baudRate">
        <el-select
          v-model="queryParams.baudRate"
          placeholder="波特率"
          @change="handleQuery" clearable id="baudRate"
        >
          <el-option
            v-for="dict in dict.type.sys_baud_rate"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据位" prop="dataBits">
        <el-select
          v-model="queryParams.dataBits"
          placeholder="数据位"
          @change="handleQuery" clearable id="dataBits"
        >
          <el-option
            v-for="dict in dict.type.sys_data_bits"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="停止位" prop="stopBits">
        <el-select
          v-model="queryParams.stopBits"
          placeholder="停止位"
          @change="handleQuery" clearable id="stopBits"
        >
          <el-option
            v-for="dict in dict.type.sys_stop_bits"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="校验位" prop="parity">
        <el-select
          v-model="queryParams.parity"
          placeholder="校验位"
          @change="handleQuery" clearable id="parity"
        >
          <el-option
            v-for="dict in dict.type.sys_parity"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="采集频率" prop="cycle">
        <el-input
          v-model="queryParams.cycle"
          placeholder="采集频率，单位：毫秒"
          clearable
          @keyup.enter.native="handleQuery"
          id="cycle"
        />
      </el-form-item>
      <el-form-item label="超时时间" prop="timeout">
        <el-input
          v-model="queryParams.timeout"
          placeholder="请输入通讯超时时间"
          clearable
          @keyup.enter.native="handleQuery"
          id="timeout"
        />
      </el-form-item>
      <el-form-item label="通讯方式" prop="comtx">
        <el-input
          v-model="queryParams.comtx"
          placeholder="串口通讯类型(rs-232,rs-485)"
          clearable
          @keyup.enter.native="handleQuery"
          id="comtx"
        />
      </el-form-item>
      <el-form-item label="网关ID" prop="macID">
        <el-input
          v-model="queryParams.macID"
          placeholder="请输入设备所属的网络网关ID"
          clearable
          @keyup.enter.native="handleQuery"
          id="macID"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dlldrinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dlldrinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dlldrinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:dlldrinfo:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dlldrinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dlldrinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="接口ID" align="center" prop="drid" />
      <el-table-column label="接口名称" align="center" prop="drname" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="接口驱动" align="center" prop="drllname" :show-overflow-tooltip="true"/>
      <el-table-column label="驱动类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dll_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="接口ip和端口" align="center" prop="ipport" :show-overflow-tooltip="true" width="180"/>
      <el-table-column label="com串口" align="center" prop="com" />
      <el-table-column label="波特率" align="center" prop="baudRate" />
      <el-table-column label="数据位" align="center" prop="dataBits" />
      <el-table-column label="停止位" align="center" prop="stopBits" />
      <el-table-column label="校验位" align="center" prop="parity">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_parity" :value="scope.row.parity"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="采集频率(单位：毫秒)" align="center" prop="cycle" />
      <el-table-column label="超时时间" align="center" prop="timeout" />
      <el-table-column label="通讯方式" align="center" prop="comtx" />-->
      <el-table-column label="网关ID" align="center" prop="macID" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dlldrinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dlldrinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport + '&dbName=' + upload.dbName"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的网关配置数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改设备采集配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="接口名称" prop="drname">
          <el-input v-model="form.drname" placeholder="请输入接口设备名称" />
        </el-form-item>
        <el-form-item label="接口驱动" prop="drllid">
          <el-select v-model="form.drllid" placeholder="请选择接口驱动" clearable>
            <el-option
              v-for="apptype in drllOptions"
              :key="apptype.drllid"
              :label="apptype.drllname"
              :value="apptype.drllid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="驱动类型" prop="type">
          <el-select v-model="form.type" placeholder="驱动类型">
            <el-option
              v-for="dict in dict.type.sys_dll_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="ip和端口" prop="ipport">
          <el-input v-model="form.ipport" placeholder="格式192.168.2.200:30" />
        </el-form-item>
        <el-form-item label="设备串口" prop="com">
          <el-select v-model="form.com" placeholder="请选择串口">
            <el-option
              v-for="dict in dict.type.sys_com"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="波特率" prop="baudRate">
          <el-select v-model="form.baudRate" placeholder="请选择波特率">
            <el-option
              v-for="dict in dict.type.sys_baud_rate"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据位" prop="dataBits">
          <el-select v-model="form.dataBits" placeholder="请选择数据位">
            <el-option
              v-for="dict in dict.type.sys_data_bits"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="停止位" prop="stopBits">
          <el-select v-model="form.stopBits" placeholder="请选择停止位">
            <el-option
              v-for="dict in dict.type.sys_stop_bits"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="校验位" prop="parity">
          <el-select v-model="form.parity" placeholder="请选择校验位">
            <el-option
              v-for="dict in dict.type.sys_parity"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="采集频率" prop="cycle">
          <el-input v-model="form.cycle" placeholder="采集频率，单位：毫秒" />
        </el-form-item>
        <el-form-item label="超时时间" prop="timeout">
          <el-input v-model="form.timeout" placeholder="请输入通讯超时时间" />
        </el-form-item>
        <el-form-item label="通讯方式" prop="comtx">
          <el-input v-model="form.comtx" placeholder="串口通讯类型名称(rs-232,rs-485)" />
        </el-form-item>-->
        <el-form-item label="网关ID" prop="macID">
          <el-input v-model="form.macID" placeholder="请输入设备所属的网络网关ID，默认1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDlldrinfo, getDlldrinfo, delDlldrinfo, addDlldrinfo, updateDlldrinfo,drllNameSelect,getDbName } from "@/api/system/dlldrinfo";
import {getToken} from "@/utils/auth";

export default {
  name: "Dlldrinfo",
  dicts: ['sys_com','sys_baud_rate', 'sys_data_bits','sys_stop_bits','sys_parity','sys_dll_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备采集配置表格数据
      dlldrinfoList: [],
      // 项目类型树选项
      drllOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        dbName : getDbName(),
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/dlldrinfo/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drname: null,
        drllid: null,
        type: null,
        ipport: null,
        com: null,
        baudRate: null,
        dataBits: null,
        stopBits: null,
        parity: null,
        cycle: null,
        timeout: null,
        comtx: null,
        macID: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDrllNameSelect();
  },
  methods: {
    /** 查询设备采集配置列表 */
    getList() {
      this.loading = true;
      listDlldrinfo(this.queryParams).then(response => {
        this.dlldrinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询接口驱动 */
    getDrllNameSelect() {
      drllNameSelect().then(response => {
        this.drllOptions = response.data;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        drid: null,
        drname: null,
        drllid: null,
        type: null,
        ipport: null,
        com: null,
        baudRate: null,
        dataBits: null,
        stopBits: null,
        parity: null,
        cycle: null,
        timeout: null,
        comtx: null,
        macID: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.drid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备采集配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const drid = row.drid || this.ids
      getDlldrinfo(drid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备采集配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.drid != null) {
            updateDlldrinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDlldrinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const drids = row.drid || this.ids;
      this.$modal.confirm('是否确认删除设备采集配置编号为"' + drids + '"的数据项？').then(function() {
        return delDlldrinfo(drids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dlldrinfo/export', {
        ...this.queryParams
      }, `项目网关配置_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "网关配置数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/dlldrinfo/importTemplate', {
      }, `dlldrinfo_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
